package com.ezcoding.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 默认日期格式
     */
    private static final String DEFAULT_FORMAT = "yyyy-MM-dd";

    private static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat(DEFAULT_FORMAT);

    public static String getCurrentDate() {
        return DEFAULT_SDF.format(new Date());
    }

    private static boolean isValid(String curDateStr, String endDateStr) {
        try {
            Date curDate = DEFAULT_SDF.parse(curDateStr);
            Date endDate = DEFAULT_SDF.parse(endDateStr);
            return curDate.before(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isValid(String endDateStr) {
        return isValid(getCurrentDate(), endDateStr);
    }

    public static String dateAdd(String dateStr, int day) {
        try {
            Date date = DEFAULT_SDF.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, day);
            Date dateRes = calendar.getTime();

            return DEFAULT_SDF.format(dateRes);
        } catch (ParseException e) {
            e.printStackTrace();
            return dateStr;
        }
    }
}
